var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
    data: {
        keywrod: '',
        searchStatus: false,
        goodsList: [],
        helpKeyword: [],
        historyKeyword: [],
        categoryFilter: false,
        currentSortType: 'default',
        filterCategory: [],
        defaultKeyword: {},
        hotKeyword: [],
        currentSortOrder: 'desc',
        salesSortOrder: 'desc',
        categoryId: 0,
        page: 1,
        showTips: 0
    },
    //事件处理函数
    closeSearch: function () {
        wx.navigateBack()
    },
    clearKeyword: function () {
        this.setData({
            keyword: '',
            searchStatus: false
        });
    },
    onLoad: function () {
        this.getSearchKeyword();
        let history = wx.getStorageSync('searchHistory');
        this.setData({
            historyKeyword: history
        })
    },
    getSearchKeyword() {
        let that = this;
        util.request(api.SearchIndex).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    // historyKeyword: res.data.historyKeywordList,
                    // defaultKeyword: res.data.defaultKeyword,
                    hotKeyword: res.data
                });
            }
        });
    },
    inputChange: function (e) {
        this.setData({
            keyword: e.detail.value,
            searchStatus: false
        });
        this.getHelpKeyword();
    },
    getHelpKeyword: function () {
        let that = this;
        util.request(api.SearchHelper, {keyword: that.data.keyword}).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    helpKeyword: res.data
                });
            }
        });
    },
    inputFocus: function () {
        this.setData({
            searchStatus: false,
            goodsList: []
        });
        if (this.data.keyword) {
            this.getHelpKeyword();
        }
    },
    clearHistory: function () {
        this.setData({
            historyKeyword: []
        });
        wx.setStorageSync('searchHistory', []);
    },
    getGoodsList: function () {
        let that = this;
        util.request(api.GoodsList, {
            keyword: that.data.keyword,
            sort: that.data.currentSortType,
            order: that.data.currentSortOrder,
            page: that.data.page
            // sales: that.data.salesSortOrder
        }).then(function (res) {
            if (res.code == 0) {
                if (res.data.length == 0) {
                    that.setData({
                        showTips: 1
                    });
                    wx.hideLoading();
                    return false;
                }
                that.setData({
                    searchStatus: true,
                    goodsList: that.data.goodsList.concat(res.data),
                });
            }
            // 重新获取关键词
            wx.hideLoading();
            that.getSearchKeyword();
        });
    },
    onKeywordTap: function (event) {
        this.getSearchResult(event.target.dataset.keyword);
    },
    getSearchResult(keyword) {
        let history = wx.getStorageSync('searchHistory') || [];
        if (history.length == 0) {
            history.push(keyword);
        } else {
            if (history.indexOf(keyword) == -1) {
                history.push(keyword);
            }
        }
        wx.setStorageSync('searchHistory', history);
        this.setData({
            keyword: keyword,
            page: 1,
            categoryId: 0,
            goodsList: [],
            historyKeyword: history,
        });
        this.getGoodsList();
    },
    openSortFilter: function (event) {
        let currentId = event.currentTarget.id;
        this.setData({
            goodsList: [],
            page: 1
        });
        switch (currentId) {
            case 'salesSort':
                let _SortOrder = 'asc';
                if (this.data.salesSortOrder == 'asc') {
                    _SortOrder = 'desc';
                }
                this.setData({
                    'currentSortType': 'sales',
                    'currentSortOrder': _SortOrder,
                    'salesSortOrder': _SortOrder
                });
                this.getGoodsList();
                break;
            case 'priceSort':
                let tmpSortOrder = 'asc';
                if (this.data.currentSortOrder == 'asc') {
                    tmpSortOrder = 'desc';
                }
                this.setData({
                    'currentSortType': 'price',
                    'currentSortOrder': tmpSortOrder,
                    'salesSortOrder': 'asc'
                });
                this.getGoodsList();
                break;
            default:
                //综合排序
                this.setData({
                    'currentSortType': 'default',
                    'currentSortOrder': 'desc',
                    'salesSortOrder': 'desc'
                });
                this.getGoodsList();
        }
    },
    onKeywordConfirm(event) {
        this.getSearchResult(event.detail.value);
    },
    onReachBottom: function() {
        let that = this;
        wx.showLoading({
            title: '',
            mask:true
        });
        that.setData({
            page: parseInt(that.data.page)+1
        });
        this.getGoodsList();
    },
})